#open(file,mode,encoding)    file:文件路径,mode:操作开方式,encoding:
# file=open("D:\工地英语.txt",'w')
# # file.write('I like python')
# # file.close()
# file_1=open("D:\工地英语.txt",'r',encoding='utf-8')
# content=file_1.read()
# print(content)
# ############
# file_1=open("D:\工地英语.txt",'rb')
# a=file.read().decode('utf-8')
# print(a)
file=open("D:\工地英语.txt",'r',encoding='utf-8')
a=file.readlines()
print(file.tell())
file.seek(3)#seek(下标) 光标位置
a=file.readline()
print(a)
file.seek(8)
c=file.readline()
print(c)
#内存假文件
#文本模式 如下
import io
i='为了联盟'
lol='我们为了联盟'
print(lol.encode(encoding='gbk'))
sio=io.StringIO()#通过这个东西创建一个虚拟的文本,文件关闭就会消失
sio.write(str(i))
a=sio.getvalue()
print(a)

with open ("D:\工地英语.txt",'r',encoding='utf-8') as file:
    f=file.read()
    print(f)
################
class Test:
    def __enter__(self):
        print('正在调用enter')
        return 'enter 返回'
    def __exit__(self, exc_type, exc_val, exc_tb):
        print('正在调用exit')

with Test() as F:
    print(1)
    print(2)

###############
# class Open:
#     def __init__(self,file):
#         self.file=file
#
#     def __enter__(self):
#         self.f=open(self,file,'r',encoding='utf-8')
#         return self.f
#
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         print('正在调用exit')
#
#
# with Open("D:\工地英语.txt",'r',encoding='utf-8') as F1:
#     a=F1.read()
#     print(a)
import OS模块
print(OS模块.getcwd())#现实路径
print(OS模块.listdir('.'))#显示当前文件夹下的所有文件
class Open:
    def __init__(self,file):
        self.file=file

    def __enter__(self):
        # self.f=open(self,file,'r',encoding='utf-8')
        self.f = open(self.file,'r',encoding='utf-8')
        return self.f

    def __exit__(self, exc_type, exc_val, exc_tb):
        print('正在调用exit')


with Open("D:\工地英语.txt") as F1:
    a=F1.read()
    print(a)













